package com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.impl;

import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.HonestGiftChildNativeRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestGiftChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.viewobjects.HonestGiftChildQueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

/**
 * @author z
 * @date 2020-12-11
 */
@Repository
public class HonestGiftChildNativeRepositoryImpl implements HonestGiftChildNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(HonestGiftChildNativeRepositoryImpl.class);


    @Override
    public Page<HonestGiftChildEntity> queryHonestGiftChilds(HonestGiftChildQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from honest_gift_child ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isEmpty(queryParams.getHonestGiftAmount())) {
            whereSql.append(" and HONEST_GIFT_AMOUNT like :honestGiftAmount");
        }
        if (!StringUtils.isEmpty(queryParams.getHonestGiftHandle())) {
            whereSql.append(" and HONEST_GIFT_HANDLE like :honestGiftHandle");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HonestGiftChildEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from honest_gift_child ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(queryParams.getHonestGiftAmount())) {
            query.setParameter("honestGiftAmount", "%" + queryParams.getHonestGiftAmount() + "%");
            countQuery.setParameter("honestGiftAmount", "%" + queryParams.getHonestGiftAmount() + "%");
        }
        if (!StringUtils.isEmpty(queryParams.getHonestGiftHandle())) {
            query.setParameter("honestGiftHandle", "%" + queryParams.getHonestGiftHandle() + "%");
            countQuery.setParameter("honestGiftHandle", "%" + queryParams.getHonestGiftHandle() + "%");
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<HonestGiftChildEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<HonestGiftChildEntity> honestGiftChildPage = new PageImpl<>(list, pageable, count.longValue());
        return honestGiftChildPage;
    }


    @Override
    public List<HonestGiftChildEntity> queryHonestGiftChild(HonestGiftChildQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from honest_gift_child ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isEmpty(queryParams.getHonestGiftAmount())) {
            whereSql.append(" and HONEST_GIFT_AMOUNT like :honestGiftAmount");
        }
        if (!StringUtils.isEmpty(queryParams.getHonestGiftHandle())) {
            whereSql.append(" and HONEST_GIFT_HANDLE like :honestGiftHandle");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HonestGiftChildEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from honest_gift_child ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(queryParams.getHonestGiftAmount())) {
            query.setParameter("honestGiftAmount", "%" + queryParams.getHonestGiftAmount() + "%");
            countQuery.setParameter("honestGiftAmount", "%" + queryParams.getHonestGiftAmount() + "%");
        }
        if (!StringUtils.isEmpty(queryParams.getHonestGiftHandle())) {
            query.setParameter("honestGiftHandle", "%" + queryParams.getHonestGiftHandle() + "%");
            countQuery.setParameter("honestGiftHandle", "%" + queryParams.getHonestGiftHandle() + "%");
        }
        return query.getResultList();
    }

    @Override
    public List<HonestGiftChildEntity> getList(String bxgdReportInfoId) {
        StringBuilder sql = new StringBuilder("select * from honest_gift_child ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isEmpty(bxgdReportInfoId)) {
            whereSql.append(" and REPORT_ID = :bxgdReportInfoId");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HonestGiftChildEntity.class);

        StringBuilder countSql = new StringBuilder("select count(*) from honest_gift_child ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(bxgdReportInfoId)) {
            query.setParameter("bxgdReportInfoId", bxgdReportInfoId);
            countQuery.setParameter("bxgdReportInfoId", bxgdReportInfoId);
        }
        List<HonestGiftChildEntity> list = query.getResultList();

//        Collections.reverse(list);
        return (null != list && list.size() > 0) ? list : null;
    }
}
